package com.service.impl;


import cn.hutool.json.JSONUtil;
import com.dao.CacheDao;
import com.dao.RedisDao;
import com.dao.TeacherDao;
import com.dto.DTO;
import com.dto.MessageEnum;
import com.entity.Sc;
import com.entity.Student;
import com.entity.Teacher;
import com.exception.SelectCourseFailException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.page.PageInfo;

import com.service.TeacherService;
import com.utils.JacksonUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherDao teacherDao;
    @Resource
    RedisDao redisDao;
    @Resource
    CacheDao cacheDao;

    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Teacher> getTeachersByPage(PageInfo pageInfo, String cacheId) throws Exception {
        List<Teacher> teachers = teacherDao.getTeacherByPage(pageInfo);
        MessageEnum messageEnum = teachers.size() > 0? MessageEnum.GETSTUDENTSUCCESS:MessageEnum.GETSTUDENTFAIL;
        DTO<Teacher> dto = new DTO(messageEnum.getCode(),messageEnum.getMessage(),teachers,pageInfo);
        return dto;
    }

    @Override
    public DTO<Teacher> getTeachersByPage(PageInfo pageInfo) throws Exception {
        List<Teacher> teachers = teacherDao.getTeacherByPage(pageInfo);
        MessageEnum messageEnum = teachers.size() > 0? MessageEnum.GETSTUDENTSUCCESS:MessageEnum.GETSTUDENTFAIL;
        DTO<Teacher> dto = new DTO(messageEnum.getCode(),messageEnum.getMessage(),teachers,pageInfo);

        return dto;
    }

    @Override
    public DTO<Teacher> editTeacher(Teacher teacher) throws Exception {
        System.out.println("增加教师授课");
        Integer result = teacherDao.editTeacher(teacher);
        DTO<Teacher> dto = result > 0 ? new DTO(MessageEnum.GETSTUDENTSUCCESS.getCode(), MessageEnum.GETSTUDENTSUCCESS.getMessage()) :
                new DTO(MessageEnum.GETSTUDENTFAIL.getCode(), MessageEnum.GETSTUDENTFAIL.getMessage());
        //System.out.println(cacheDao.getInCacheCode(teacher.getTno()));
        //redisDao.delete("");
        ObjectMapper mapper = new ObjectMapper();
        boolean inRedisFlag = false;//默认不在redis的某一页缓存中
        for(int i=0;i<2;i++) {
            //redisDao会从redis中取出TeacherBYPage1对应的json字符串，利用自己写的JacksonUtils工具deserializeDTO反序列化出dto对象
            String jsonStr = redisDao.get("TeacherByPage"+(i+1));
            System.out.println("----------------------");
            System.out.println(jsonStr);
            DTO<Teacher> redisBackDTO = JacksonUtils.deserializeDTO(jsonStr, DTO.class, Teacher.class);
            if(redisBackDTO.getCode() == 200) {//因为反序列化保存的dto对象的值一定是200，能够找到缓存状态吗就是200 而没有找到缓存的状态吗409
                for (Teacher teacherTemp : redisBackDTO.gettList()) {
                    if (teacher.getTno().equals(teacherTemp.getTno())) {//如果判断成立 则本次修改的数据在缓存中需要删除原来的缓存让下一次查询自动更新
                        redisDao.delete("TeacherByPage" + (i + 1));
                        inRedisFlag = true;
                        break;
                    }
                }
                if (inRedisFlag) {
                    break;
                }
            }
        }
        return dto;
    }


}
